package com.zxy.salenet.web.web.controller;

import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.pojo.SalenetUser;
import com.zxy.salenet.web.service.AdminUserOperationService;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: zxy
 * @version: 1.0
 * @description: 管理员对用户操作控制层
 */
  /**
 * AdminUserOperationController.
 */
@Api(value = "管理员管理用户API",tags = "管理员管理用户API")
@RestController
@RequestMapping(path = "/adminOperationUser")
public class AdminUserOperationController {
    @Autowired
    private AdminUserOperationService adminUserOperationService;


    /**
     * 获取所有的用户信息
     *
     * 需要查询的用户名
     * 当前页数       默认当前页为第一页
     * 一页显示条数   默认10条
     * @return
     */
    @ApiOperation(value = "获取所有的用户信息 如果userName参数不给数据的话 则获取所有的数据 pageNum 当前页 pageSize一页显示多少条数据",notes = "获取所有的用户信息 数据格式JSON {\"userName\":\"\",\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "queryAllUserList")
    private Json queryAllUser(@RequestBody Map<String,String> map) {
        Json json = new Json();
        System.out.println(map);
        String userName = map.get("userName");
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageInfo<SalenetUser> userPageList = adminUserOperationService.querAllUserByName(userName, pageNum, pageSize);
        json.setCode(200);
        json.setData(userPageList);
        json.setMessage("查询成功");
        return json;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "管理员修改用户的信息",notes = "修改用户信息 数据格式 JSON {\n" +
            "    \"id\": 1,\n" +
            "                \"userId\": 67531,\n" +
            "                \"userRealName\": \"李珏\",\n" +
            "                \"userName\": \"韦德de忠实粉丝\",\n" +
            "                \"userSex\": \"男\",\n" +
            "                \"userAge\": 22,\n" +
            "                \"userAddress\": \"河南省郑州市金水区金碧苑3301\",\n" +
            "                \"userBrith\": \"1998-02-01T16:00:00.000+0000\",\n" +
            "                \"userPasswd\": \"13ea44c0a4468f1c0c16c89fa76b58d5\",\n" +
            "                \"userPhone\": \"13097814606\",\n" +
            "                \"userEmail\": \"123@qq.com\",\n" +
            "                \"userPaypass\": \"41287e3325f29e679a5c1a199813cbc0\",\n" +
            "                \"userComm\": 1\n" +
            "}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/modifyUser")
    private Json modifyUserMessage(@RequestBody SalenetUser user) {
        Json json = new Json();
        Integer updateResult = adminUserOperationService.updateUserMessage(user);
        if (updateResult == 0){
            json.setData(0);
            json.setCode(800);
            json.setMessage("修改失败");
            return json;
        }
        json.setCode(200);
        json.setData(1);
        json.setMessage("修改成功");
        return json;
    }

    /**
     * 根据用户id删除用户
     * @return
     */
    @ApiOperation(value = "根据userId删除对应用户",notes = "发送用户的userId 数据格式JSON {\"userId\":67531}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "deleteUser")
    private Json deleteUser(@RequestBody Map<String,Integer> maps) {
        Json json = new Json();
        Integer userId = maps.get("userId");
        System.out.println(userId);
        Integer deleteResult = adminUserOperationService.deleteUserByUserId(userId);
        if(deleteResult == 0){
            json.setCode(800);
            json.setData(0);
            json.setMessage("删除失败");
            return json;
        }
        json.setCode(200);
        json.setData(1);
        json.setMessage("删除成功");
        return json;
    }
}
